package com.central.apps.handler.MaterialInventory;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 退库管理
 *
 */
@Component
public class materialreturnHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;


    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        //领料单行信息接口
        if("getlldLine".equals(type)){
            String ordenum = MapUtil.getStr(params, "ordenum");//领料单编码
            String sql =" select * from bl_LLDLINE where LLDNUM in (select lldnum from bl_lld where lldnum='"+ordenum+"' and status='已出库')";
            return BLuntil.toPage(params,handlerService,sql);

        }

        return super.operate(app, type, params);
    }

    /**
     * 自定义审批人
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        //定义一个存放人员的字符串
        String executer = null;
        List<Map<String, Object>> list = handlerService.findBySql
                (" select * from bl_warehouse where warehousenum = (select location from bl_materialreturn where id='"+id+"')");
//        String Checkperson = MapUtil.getStr(list.get(0), "LOCATION");//盘点人
        String managernum= MapUtils.getString(list.get(0),"managernum");//仓库管理员


        //库房管理员
        if (flowNode.getId().equals("kfgly")) {
            if (managernum!=null&&!managernum.equals("")){
                executer=managernum;
            }
        }

        return executer;
    }


    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("kfgly")){
            //审核通过后修改库存预留数量
            boolean flag = op.isFlag();//判断是否审核通过
            //审核通过的情况
            if(flag) {
                List<Map<String, Object>> list = handlerService.findBySql("select * from bl_materialreturn where id=" + id);
                String location = MapUtil.getStr(list.get(0), "LOCATION");//仓库号
                String num = MapUtil.getStr(list.get(0), "BILLNUM");//退库单编码
                String ordenum = MapUtil.getStr(list.get(0), "ordenum");//领料单单编码
                List<Map<String, Object>> lineList = handlerService.findBySql("select *from bl_materialreturline where BILLNUM ='"+num+"'");
                if (lineList != null) {
                    // 更新领料单状态
                    handlerService.findBySql("update bl_lld set status='已退库' where lldnum='"+ordenum+"'");
                    for (int k = 0; k < lineList.size(); k++) {
                        String itemnum = MapUtil.getStr(lineList.get(k), "ITEMNUM");//物料编号
                        Integer transfernum = MapUtil.getInt(lineList.get(k), "BUNBER");//退料数量
                        // 更新库存
                        handlerService.findBySql("update  bl_Inventorys  set actqty=actqty+"+transfernum+",currentqty=currentqty+"+transfernum+" where itemnum ='"+itemnum+"' and warehousenum ='"+location+"'");

                    }
                }

            }
        }
    }
}
